#include "ds1302.h"

u8 bdata com;
sbit BUZZER = com^6;
sbit RELAY = com^4;
bit disp_status;
u8 key_value = 0,key_up = 1;
u8 t_h = 23,t_m = 59,t_s = 50,a_h = 0,a_m = 0,a_s = 0;
u8 t_flag,a_flag,led_flag,flag,tem_flag;

void buzzer(u8 cmd)
{
    BUZZER = cmd;
    P0 = com;
    P2 = 0xa0;
    _nop_();
    P2 = 0x00;
}

void relay(u8 cmd)
{
    RELAY = cmd;
    P0 = com;
    P2 = 0xa0;
    _nop_();
    P2 = 0x00;
}

void led(u8 ucode)
{
    P0 = ~ucode;
    P2 = 0x80;
    _nop_();
    P2 = 0x00;
}

void smg_disp(u8 num,u8 position)
{
    P0 = 0xff;
    P2 = 0xe0;
    _nop_();
    P2 = 0x00;

    P0 = position;
    P2 = 0xc0;
    _nop_();
    P2 = 0x00;

    P0 = ~num;
    P2 = 0xe0;
    _nop_();
    P2 = 0x00;
}

void key_scan(void)
{
	u8 temp;
    if(key_up&&(P33==0||P32==0||P31==0||P30==0))
    {
        key_up = 0;
        if(!P33)                //S4
        {
            if(led_flag == 1)
			{
				led_flag = 2;
				return ;
			}
            if(disp_status == 0 && t_flag)
            {
                if(t_flag == 1 && t_h > 0)
				{
					t_h--;
					temp = ((t_h/10)<<4) + t_h%10;
					Write_Ds1302_Byte(0x8e,0x00);
					Write_Ds1302_Byte(0x84,temp);
					Write_Ds1302_Byte(0x8e,0x80);
				}  
                else if(t_flag == 2 && t_m > 0)
                {
					t_m--;
					temp = ((t_m/10)<<4) + t_m%10;
					Write_Ds1302_Byte(0x8e,0x00);
					Write_Ds1302_Byte(0x82,temp);
					Write_Ds1302_Byte(0x8e,0x80);
				}
                else if(t_flag == 3 && t_s > 0)
                {
					t_s--;
					temp = ((t_s/10)<<4) + t_s%10;
					Write_Ds1302_Byte(0x8e,0x00);
					Write_Ds1302_Byte(0x80,temp);
					Write_Ds1302_Byte(0x8e,0x80);
				}
            }
            else if(disp_status == 1 && a_flag)
            {
                if(a_flag == 1 && a_h > 0)
                    a_h--;
                else if(a_flag == 2 && a_m > 0)
                    a_m--;
                else if(a_flag == 3 && a_s > 0)
                    a_s--;
            }
            else if(disp_status == 0)
            {
                tem_flag = 1;
            }
        }
        else if(!P32)           //S5
        {
            if(led_flag == 1)
            {
				led_flag = 2;
				return ;
			}
            if(disp_status == 0 && t_flag)
            {
                if(t_flag == 1 && t_h < 23)
				{
					t_h++;
					temp = ((t_h/10)<<4) + t_h%10;
					Write_Ds1302_Byte(0x8e,0x00);
					Write_Ds1302_Byte(0x84,temp);
					Write_Ds1302_Byte(0x8e,0x80);
				}
                else if(t_flag == 2 && t_m < 59)
				{
					t_m++;
					temp = ((t_m/10)<<4) + t_m%10;
					Write_Ds1302_Byte(0x8e,0x00);
					Write_Ds1302_Byte(0x82,temp);
					Write_Ds1302_Byte(0x8e,0x80);
				}
                else if(t_flag == 3 && t_s < 59)
				{
					t_s++;
					temp = ((t_s/10)<<4) + t_s%10;
					Write_Ds1302_Byte(0x8e,0x00);
					Write_Ds1302_Byte(0x80,temp);
					Write_Ds1302_Byte(0x8e,0x80);
				}
            }
            else if(disp_status == 1 && a_flag)
            {
                if(a_flag == 1 && a_h < 23)
                    a_h++;
                else if(a_flag == 2 && a_m < 59)
                    a_m++;
                else if(a_flag == 3 && a_s < 59)
                    a_s++;
            }
        }
        else if(!P31)           //S6
        {
            if(led_flag == 1)
            {
				led_flag = 2;
				return ;
			}
            if(!disp_status && !t_flag)
                disp_status = 1;
            
            if(disp_status)
            {
                t_flag = 0;
                if(++a_flag == 4)
                {
                    a_flag = 0;
                    disp_status = 0;                
                }
                else
                {
                    if(t_s % 2)
                        flag = 1;
                    else
                        flag = 0;
                }
            }
        }
        else if(!P30)       //S7
        {
            if(led_flag == 1)
            {
				led_flag = 2;
				return ;
			}
			if(!disp_status)
            {
				a_flag = 0;
				if(++t_flag == 4)
						t_flag = 0;
				else
				{
					if(t_s % 2)
						flag = 1;
					else
						flag = 0;
				}
			}
        }
    }
    else if(P33&&P32&&P31&&P30)
        key_up = 1;
}